

package com.loong.loong.hik.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loong.loong.common.core.util.R;
import com.loong.loong.common.excel.annotation.ResponseExcel;
import com.loong.loong.common.log.annotation.SysLog;
import com.loong.loong.hik.entity.Alarm;
import com.loong.loong.hik.service.AlarmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 报警输出
 *
 * @author jinyiang
 * @date 2023-03-29 14:01:35
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/alarm" )
@Api(value = "alarm", tags = "报警输出管理")
public class AlarmController {

    private final  AlarmService alarmService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param alarm 报警输出
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('hik_alarm_view')" )
    public R getAlarmPage(Page page, Alarm alarm) {
		return R.ok(alarmService.page(page,
				Wrappers.query(alarm).lambda()
						.like(StringUtils.isNotEmpty(alarm.getLikeName()), Alarm::getAlarmName,alarm.getLikeName())
						.between(StringUtils.isNotEmpty(alarm.getStartTime())&&StringUtils.isNotEmpty(alarm.getEndTime()),Alarm::getCreateTime,alarm.getStartTime(),alarm.getEndTime())));
    }


    /**
     * 通过id查询报警输出
     * @param alarmId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{alarmId}" )
    @PreAuthorize("@pms.hasPermission('hik_alarm_view')" )
    public R getById(@PathVariable("alarmId" ) Integer alarmId) {
        return R.ok(alarmService.getById(alarmId));
    }

    /**
     * 新增报警输出
     * @param alarm 报警输出
     * @return R
     */
    @ApiOperation(value = "新增报警输出", notes = "新增报警输出")
    @SysLog("新增报警输出" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('hik_alarm_add')" )
    public R save(@RequestBody Alarm alarm) {
        return R.ok(alarmService.save(alarm));
    }

    /**
     * 修改报警输出
     * @param alarm 报警输出
     * @return R
     */
    @ApiOperation(value = "修改报警输出", notes = "修改报警输出")
    @SysLog("修改报警输出" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('hik_alarm_edit')" )
    public R updateById(@RequestBody Alarm alarm) {
        return R.ok(alarmService.updateById(alarm));
    }

    /**
     * 通过id删除报警输出
     * @param alarmId id
     * @return R
     */
    @ApiOperation(value = "通过id删除报警输出", notes = "通过id删除报警输出")
    @SysLog("通过id删除报警输出" )
    @DeleteMapping("/{alarmId}" )
    @PreAuthorize("@pms.hasPermission('hik_alarm_del')" )
    public R removeById(@PathVariable Integer alarmId) {
        return R.ok(alarmService.removeById(alarmId));
    }


    /**
     * 导出excel 表格
     * @param alarm 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('hik_alarm_export')" )
    public List<Alarm> export(Alarm alarm) {
		return alarmService.list(
				Wrappers.query(alarm).lambda()
						.like(StringUtils.isNotEmpty(alarm.getLikeName()), Alarm::getAlarmName,alarm.getLikeName())
						.between(StringUtils.isNotEmpty(alarm.getStartTime())&&StringUtils.isNotEmpty(alarm.getEndTime()),Alarm::getCreateTime,alarm.getStartTime(),alarm.getEndTime()));
    }
}
